using System;
using System.Collections;
using System.Collections.Generic;
using Game.data.PlayerData;
using GameFramework.common;
using GameFramework.events;
using GameFramework.scene;
using GameFramework.timer;
using GameFramework.UI;
using UnityEngine;
using UnityEngine.UIElements;
using Event = UnityEngine.Event;

namespace Game.ui
{
    [View(SceneViewManagerName = SceneViewManagerNames.Home)]
    [Presenter(type = typeof(WarPresenter))]
    public class WarView : BaseView
    {

        [UIBind(ClassName = "judianList")]
        private ListView judianList;

        [UIBind(ClassName = "exit")]
        private Label exit;
        [UIBind(ClassName = "tiaozhan")]
        private Button tiaozhan;

        private List<JudianData> _judianDatas;
        private List<GuanQiaData> guanQiaDatas;//所有的关卡数据
        private List<GuanQiaData> nowGuanQiaDatas;//当前选中据点的关卡数据
        public override void init()
        {

        }

        public override void beforeShow()
        {
            base.beforeShow();
            // TODO 返回控件做成组件
            elementRegisterCallBack<ClickEvent>(exit, e =>
            {
                Debug.Log("退出点击！");
                UIManager.Instance.show<HomeView>();
            });
            
            elementRegisterCallBack<ClickEvent>(tiaozhan, e => tiaozhanGuanQia());
        }

        private void tiaozhanGuanQia()
        {
            SceneLoadingManager.Instance.LoadingSceneWithLoadingUI("Fight", () =>
            {
                Debug.Log("显示战斗UI");
                UIManager.Instance.show<FightMainView>();
            });

        }

        protected override void modelPrepared(object data)
        {
            if (!(data is WarData))
            {
                Debug.LogError("modelPrepared: data is not WarData");
                return;
            }

            //初始化战役
            WarData warData = data as WarData;

            _judianDatas = warData.judians;
            Debug.Log("据点数据："+_judianDatas.Count);
            
            judianList.bindItem = judianListBindItem;
            judianList.makeItem = judianListMakeItem;
            judianList.itemsSource = _judianDatas;
            //设置当前选中的据点
        }

        private void judianListBindItem(VisualElement element, int i)
        {
            JudianItem judianItem = element as JudianItem;
            judianItem.JudianName = _judianDatas[i].name;
            judianItem.userData = _judianDatas[i];
            judianItem.judianClick += judianItemClick;
        }
        private VisualElement judianListMakeItem()
        {
            return new JudianItem();
        }

        private void judianItemClick(VisualElement el)
        {
            object userData = el.userData;
            if (!(userData is JudianData))
            {
                return;
            }

            JudianData judianData = userData as JudianData;
            if (judianData.locked)
            {
                //TODO 飘字提示
                Debug.LogWarning("据点还未解锁！");
                return;
            }
            
        }
    }
}


